from fastapi import FastAPI
from pydantic import BaseModel
from typing import Optional, Union
from __log__ import logger as log
# from middleware import log_requests_middleware  # 导入中间件
from settings import BASE_DIR
import sys

# sys.path.append(BASE_DIR)
# log setting
log = log

app = FastAPI()

# 添加中间件
# app.middleware("http")(log_requests_middleware)
# 定义统一的响应模型
class BaseResponse(BaseModel):
    success: bool = True
    code: int = 200
    data: Optional[Union[dict, list[dict]]]=None
    message: Optional[str] = None

# 定义统一的返回模型
def ReturnResponse(code: int=200, success: bool=True, message: Optional[str]=None, data: Optional[Union[dict, list[dict]]]=None):
    return {"code": code, "success": success, "message": message, "data": data}

"""==== API 路由 ===="""
# 单元测试
from test.test import router as test_router
# app.include_router(test_router, prefix="/test")  # 挂载路由，并添加前缀 /api
app.include_router(test_router)

from body.views import router as body
app.include_router(body) 